package com.example.smartcity_poverty.Base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


/**
 * 封装RecyclerView
 *
 * 使用ViewBinding前我们需要在build.gradle中启用
 *     viewBinding {
 *        enabled = true
 *     }
 */
abstract class NewRecyclerAdapter<E, VB : ViewBinding>(
    val list: MutableList<E>, // 数据源
    val layoutID: Int // 布局ID
) : RecyclerView.Adapter<NewRecyclerAdapter<E, VB>.ViewHolder>() {

    // 填充数据
    abstract fun setData(mBinding: VB, data: E, position: Int, holder: ViewHolder)

    // 获取布局
    abstract fun getInflate(layoutInflater: LayoutInflater, parent: ViewGroup): VB

    override fun onBindViewHolder(holder: ViewHolder, position: Int) =
        holder.bindData(list[position], position)

    // 列表数量
    override fun getItemCount(): Int = list.size

    // 布局Id
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(getInflate(LayoutInflater.from(parent.context), parent))

    inner class ViewHolder(private val viewBinding: VB) : RecyclerView.ViewHolder(viewBinding.root) {
        fun bindData(data: E, position: Int) {
            setData(viewBinding, data, position, this)
        }
    }
}